﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Toolkit.Web.ExtensionMethods
{
    public static class DecimalExtensions
    {
        public static string ToCurrencyString(this decimal value)
        {
            return ToCurrencyString(value, null);
        }

        public static string ToCurrencyString(this decimal value, int? precision)
        {
            return ToCurrencyString(value, precision, false);
        }

        public static string ToCurrencyString(this decimal value, bool excludeSymbol)
        {
            return ToCurrencyString(value, null, excludeSymbol);
        }

        public static string ToCurrencyString(this decimal value, int? precision, bool excludeSymbol)
        {
            CultureInfo culture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();

            if (excludeSymbol)
            {
                culture.NumberFormat.CurrencySymbol = string.Empty;
            }

            string formattedValue = string.Empty;

            if (precision.HasValue)
            {
                formattedValue = value.ToString("C" + precision.Value, culture);
            }
            else
            {
                formattedValue = value.ToString("C", culture);
            }

            return formattedValue.Trim();
        }

        public static string ToCurrencyString(this decimal? value)
        {
            return ToCurrencyString(value, null, null);
        }

        public static string ToCurrencyString(this decimal? value, int? precision)
        {
            return ToCurrencyString(value, precision, null);
        }

        public static string ToCurrencyString(this decimal? value, string defaultValue)
        {
            return ToCurrencyString(value, null, defaultValue);
        }

        public static string ToCurrencyString(this decimal? value, int? precision, string defaultValue)
        {
            return ToCurrencyString(value, precision, false, defaultValue);
        }

        public static string ToCurrencyString(this decimal? value, bool excludeSymbol, string defaultValue)
        {
            return ToCurrencyString(value, null, excludeSymbol, defaultValue);
        }

        public static string ToCurrencyString(this decimal? value, int? precision, bool excludeSymbol, string defaultValue)
        {
            if (value.HasValue)
            {
                return ToCurrencyString(value.Value, precision, excludeSymbol);
            }
            return defaultValue;
        }

        public static string ToPercentString(this decimal value)
        {
            return ToPercentString(value, null);
        }

        public static string ToPercentString(this decimal value, int? precision)
        {
            return ToPercentString(value, precision, false);
        }

        public static string ToPercentString(this decimal value, bool excludeSymbol)
        {
            return ToPercentString(value, null, excludeSymbol);
        }

        public static string ToPercentString(this decimal value, int? precision, bool excludeSymbol)
        {
            CultureInfo culture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();

            if (excludeSymbol)
            {
                culture.NumberFormat.CurrencySymbol = string.Empty;
            }

            string formattedValue = string.Empty;

            if (precision.HasValue)
            {
                formattedValue = value.ToString("P" + precision.Value, culture);
            }
            else
            {
                formattedValue = value.ToString("P", culture);
            }

            return formattedValue.Trim();
        }

        public static string ToPercentString(this decimal? value)
        {
            return ToPercentString(value, null, null);
        }

        public static string ToPercentString(this decimal? value, int? precision)
        {
            return ToPercentString(value, precision, null);
        }

        public static string ToPercentString(this decimal? value, string defaultValue)
        {
            return ToPercentString(value, null, defaultValue);
        }

        public static string ToPercentString(this decimal? value, int? precision, string defaultValue)
        {
            return ToPercentString(value, precision, false, defaultValue);
        }

        public static string ToPercentString(this decimal? value, bool excludeSymbol, string defaultValue)
        {
            return ToPercentString(value, null, excludeSymbol, defaultValue);
        }

        public static string ToPercentString(this decimal? value, int? precision, bool excludeSymbol, string defaultValue)
        {
            if (value.HasValue)
            {
                return ToPercentString(value.Value, precision, excludeSymbol);
            }
            return defaultValue;
        }

        public static string ToNumericString(this decimal value)
        {
            return ToNumericString(value, null);
        }

        public static string ToNumericString(this decimal value, int? precision)
        {
            if (precision.HasValue)
            {
                return value.ToString("N" + precision.Value);
            }
            else
            {
                return value.ToString("N");
            }
        }

        public static string ToNumericString(this decimal? value)
        {
            return ToNumericString(value, null, null);
        }

        public static string ToNumericString(this decimal? value, string defaultValue)
        {
            return ToCurrencyString(value.Value, null, defaultValue);
        }

        public static string ToNumericString(this decimal? value, int? precision)
        {
            return ToCurrencyString(value.Value, precision, null);
        }

        public static string ToNumericString(this decimal? value, int? precision, string defaultValue)
        {
            if (value.HasValue)
            {
                return ToCurrencyString(value.Value, precision);
            }
            return defaultValue;
        }
    }
}
